package com.mjf.web.controller.common;

import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.service.*;
import com.mjf.common.core.domain.AjaxResult;
import com.mjf.common.utils.ResponseVO;
import com.mjf.common.utils.ResponseUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/4/11 13:46
 */
@RestController
@RequestMapping("/tm/async")
public class TMAsyncController {

    @Autowired
    private CjtAuthService cjtAuthService;
    @Autowired
    private VoucherService voucherService;
    @Autowired
    private TmProjectDicService tmProjectDicService;
    @Autowired
    private TmAccountDicService tmAccountDicService;
    @Autowired
    private TmDepartmentDicService tmDepartmentDicService;
    @Autowired
    private TFSalesOrderService salesOrderService;
    @Autowired
    private IMjfManufactureOrderService mjfManufactureOrderService;
    @Autowired
    private TFwarehouseService tFwarehouseService;
    @Autowired
    private TFstockService tFstockService;
    @Autowired
    private TFcontactUnitService tFcontactUnitService;
    @Autowired
    private TFsalerService tFsalerService;

    @GetMapping("/token")
    @ApiOperation(value = "根据orgId获取畅捷通token", notes = "根据orgId获取畅捷通token")
    @ResponseBody
    public AjaxResult doCjtToken(@RequestParam(value = "orgId") String orgId) {
        try {
            return AjaxResult.success("执行成功", cjtAuthService.getToken(orgId));
        } catch (Exception e) {
            e.printStackTrace();

        }
        return AjaxResult.error("执行失败");
    }
    /**
     * 仓库档案
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/cw")
    public Object asyncCW() {
        tFwarehouseService.asyncCW();
        return "SUCCESS";
    }
    /**
     * 存货
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/ch")
    public Object asyncCH() {
        tFstockService.asyncCH();
        return "SUCCESS";
    }
    /**
     * 往来单位
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/wldw")
    public Object asyncWLDW() {
        tFcontactUnitService.asyncWLDW();
        return "SUCCESS";
    }
    /**
     * 同步生产工单
     * @param start
     * @param end
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/scjgd")
    public AjaxResult asyncC(@RequestParam(required = false) String start, @RequestParam(required = false) String end) throws ChanjetApiException {
        mjfManufactureOrderService.getProductionRemote(start, end);
        return AjaxResult.success();
    }

    /**
     * 同步销货单
     * @param start
     * @param end
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/xhd")
    public AjaxResult asyncXhd(@RequestParam(required = false) String start,@RequestParam(required = false) String end) throws ChanjetApiException {
        salesOrderService.asyncSaleDeliveryOrderFromRemote(start, end);
        return AjaxResult.success();
    }
    /**
     * 同步凭证
     * @param start 开始日期
     * @param end 结束日期
     * @return
     */
    @GetMapping("/voucher")
    public ResponseVO<Void> syncVoucher(@RequestParam("start") String start, @RequestParam("end") String end) {
        voucherService.syncVoucher(start, end);
        return ResponseUtils.success();
    }

    /**
     * 同步会计科目
     * @return
     */
    @GetMapping("/account")
    public ResponseVO<Void> syncAccount() {
        tmAccountDicService.syncAccount();
        return ResponseUtils.success();
    }

    /**
     * 同步项目档案
     * @return
     */
    @GetMapping("/project")
    public ResponseVO<Void> syncProject() {
        tmProjectDicService.syncProject();
        return ResponseUtils.success();
    }
    /**
     * 同步部门档案
     * @return
     */
    @GetMapping("/depart")
    public ResponseVO<Void> syncDepart() {
        tmDepartmentDicService.syncDepart();
        return ResponseUtils.success();
    }
    /**
     * 员工
     * @return
     * @throws ChanjetApiException
     */
    @GetMapping(value="/yg")
    public Object asyncYG() {
        tFsalerService.asyncYG();
        return "SUCCESS";
    }

}
